<template>
  <div id="Home">
    <el-container class="home-container">
      <!-- 页面头部-->
      <el-header>
        <div>
          <img class="headerimg" src="../assets/touxiang.png" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="loginout">退出</el-button>
      </el-header>
      <!-- 页面主体内容-->
      <el-container>
        <!-- 左侧边栏-->
        <el-aside :width="iscollapse ? '64px' : '200px'">
          <!-- 绑定一个布尔值，是ture就64px，false就200px-->
          <div class="toggel-button" @click="toggelBuotton">|||</div>
          <!--菜单栏-->
          <el-menu
            background-color="#313743"
            text-color="#fff"
            active-text-color="#409bff"
            unique-opened
            :collapse="iscollapse"
            :collapse-transition="false"
            router
            :default-active="activePath" 
          >
            <!--一级菜单-->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menulist"
              :key="item.id"
            >
              <!--一级菜单模板区域-->
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!--二级菜单-->
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
              >
                <template slot="title">
                  <i :class="i[subItem.id]"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右主内容-->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  name: 'home',
  // 监听路由地址变化传给activePath ,并且保存到会话存储中。
  watch: {
    $route (to, from) {
      window.sessionStorage.setItem('activePath', to.path)
      this.activePath = window.sessionStorage.getItem('activePath')
    }
  },
  data () {
    return {
      // 存放获取的菜单栏
      menulist: [],
      // 通过数组里的id给对应的图标
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      i: {
        110: 'el-icon-menu',
        111: 'el-icon-menu',
        112: 'el-icon-menu',
        104: 'el-icon-menu',
        115: 'el-icon-menu',
        121: 'el-icon-menu',
        107: 'el-icon-menu',
        146: 'el-icon-menu'
      },
      // 是否折叠收起菜单
      iscollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    // 让activePath = 获取到activePath的值
    // this.activePath = window.sessionStorage.getItem('activePath')
    // let router_path = this.$route.path
    // console.log(router_path)
    // this.activePath = router_path

    // 防止刷新没有跳到当前的菜单。
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // axios请求menus接口 获取所有菜单，然后传给data的menulist
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      // 使用console查看返回的函数。
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 单击按钮，是否折叠收起菜单，每次单击取反！
    toggelBuotton () {
      this.iscollapse = !this.iscollapse
    }
    // 保存链接的激活状态
    /* saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
     }, */
  }
}
</script>

<style lang="less" scoped>
#Home {
  height: 100%;
}
.home-container {
  height: 100%;
}

.el-header {
  background-color: rgb(32, 34, 37);
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;

  > div {
    //嵌套
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #313743;
  
  
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: rgb(225, 225, 236);
  
}
.headerimg {
  border-radius: 50%;
}
.iconfont {
  margin-right: 10px;
}
//当鼠标单击二级菜单时背景和文本的颜色
/*.el-menu-item.is-active {
  background-color:darkgreen !important;
  color: #fff;
  span {        color:#409bff !important;  }
}*/

.toggel-button {
  background-color: rgb(72, 86, 102);
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  color: aliceblue;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
